package cn.easytogo.action.admin;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.easytogo.action.BaseAction;
import cn.easytogo.bean.Resource;
import cn.easytogo.bean.Role;
import cn.easytogo.service.IRoleDAO;

public class RoleAction  extends BaseAction{
	private IRoleDAO roleDAO;
	private String name;
	private String admin;
	private String id;
	private List roleList;
	private String resourceIds;
	/**
	 * 前往角色列表维护页面
	 * */
	public String toList(){
		Map map = new HashMap();
		map.put("admin", admin);
		try {
			roleList = roleDAO.selectRolesByMap(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "to_list";
	}
	/**
	 * 前往角色新加页面
	 * */
	public String toNew(){
		return "to_new";
	}
	/**
	 * 功能列表页面
	 * */
	public String resourceList(){
		return "to_resource_list";
	}
	/**
	 * 保存角色
	 * */
	public void saveNew(){
		Role role = new Role();
		role.setAdmin(admin);
		role.setName(name);
		role.setResourceIds(resourceIds);
		try {
			roleDAO.insertRole(role);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			print("failed");
		}
	}
	/**
	 * 前往角色修改页面
	 * */
	public String toUpdate(){
		Map map = new HashMap();
		map.put("id", id);
		try {
			List l = roleDAO.selectRolesByMap(map);
			if(l!=null&&l.size()>0){
				Role role = (Role) l.get(0);
				name = role.getName();
				resourceIds = role.getResourceIds();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "to_new";
	}
	/**
	 * 更新角色
	 * */
	public void saveUpdate(){
		if(id!=null&&!"".equals(id)){
			Role role = new Role();
			role.setAdmin(admin);
			role.setId(id);
			role.setName(name);
			role.setResourceIds(resourceIds);
			try {
				roleDAO.updateRole(role);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				print("failed");
				return;
			}
		}
	}
	/**
	 * 删除角色
	 * */
	public void delete(){
		if(id!=null&&!"".equals(id)){
			Role role = new Role();
			role.setId(id);
			try {
				roleDAO.deleteRole(role);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				print("failed");
				return;
			}
		}
	}
	public IRoleDAO getRoleDAO() {
		return roleDAO;
	}
	public void setRoleDAO(IRoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAdmin() {
		return admin;
	}
	public void setAdmin(String admin) {
		this.admin = admin;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}
	public String getResourceIds() {
		return resourceIds;
	}
	public void setResourceIds(String resourceIds) {
		this.resourceIds = resourceIds;
	}
	
}
